
#import "wooddoc.h"
#import "ScrollViewDeluxeForOOE.h"

@implementation WoodDoc (OOE_Server)

- connectToClient:client;
{
	id connectionToClient;
	const char *path = NULL;
	
    isOOEServer = YES;
	theOOEClient = client;
    connectionToClient = [client connectionForProxy];
    [connectionToClient registerForInvalidationNotification:self];
    [client setProtocolForProxy:@protocol(OOE_ClientInterface)];
	path = [client OOE_documentPath:self];
	if(path)
		theOOEClientPath = NXCopyStringBuffer(path);
	[[scrollView theOOEButton] setEnabled:YES];	
	return self;
}

- senderIsInvalid:sender;
{
	theOOEClient = nil;
	[[scrollView theOOEButton] setEnabled:NO];
    return self;
}

- (int)OOE_isDocEdited;
{
	return [window isDocEdited]; 
}

- (int)OOE_orderDocumentFront:sender;
{   
	[NXApp activateSelf:YES];
	[window makeKeyAndOrderFront:nil];
	return 1;
}

- (int)OOE_getSize:(NXSize *)aSize;
{
	NXRect aRect;
	
	[self calculateBoundingBox:&aRect];
    NXInsetRect(&aRect, -5, -5);
	*aSize = aRect.size;	
	return 1;
}

- calculateBoundingBox:(NXRect *)aRect;
{
	float lh,lw;
	
	[tree getTreeBounds:aRect lowerWidth:&lw lowerHeight:&lh];
	return self;
}

- (void)OOE_closeDocument:sender;
{
	isOOEServer = NO;
	[self clean:self];
	[self close:self];
	return; 
}

- (int)OOE_document:client savedAs:(const char *)path;
{
	if(theOOEClientPath != NULL) 
		NX_FREE(theOOEClientPath);
	if(path == NULL)
		path = "UNTITLED";
	theOOEClientPath = NXCopyStringBuffer(path);
	return 1;
}

- (int)OOE_provideData:sender;
{
	id pboard = nil;
	
	pboard = [Pasteboard newName:OOE_PASTEBOARD];
    [self declareOOEPasteboard:pboard];
	[self writeDataToPasteboard:pboard];
	[self writePostScriptToPasteboard:pboard];
	[window setDocEdited:NO];	
	return 1;
}

- orderClientDocumentFront:sender;
{
	[theOOEClient OOE_orderClientDocumentFront:self];
	return self;
}

- (BOOL)isOOEServer
{
	return isOOEServer;
}

- setTitle_OOE:sender;
{
	char buf[MAXPATHLEN+1];
	
	if(theOOEClient == nil && isOOEServer)
		strcpy(buf, "Embedded: **** DEAD EDITOR ****");	
	else if(theOOEClientPath == NULL)
		strcpy(buf, "Embedded: UNTITLED");	
	else
		sprintf(buf, "Embedded: %s", theOOEClientPath);
	[[window contentView] ooeStatusChanged:self];	// OOE 2.0
	[window setTitle:buf];
	return self;
}

- docWillClose_OOE:sender;
{
	if(theOOEClient == nil) 
		return self;
	if(!isOOEServer) 
		return self;
	[[theOOEClient connectionForProxy] 
		unregisterForInvalidationNotification:self];
	NX_DURING
		[theOOEClient OOE_documentWillClose:self];	
	NX_HANDLER
		NXLogError("DOC %s, OOE: Error %d for "
				"'OOE_documentWillClose:'",
				[self name],
				NXLocalHandler.code);
	NX_ENDHANDLER
	[NXConnection removeObject:self];
	return self;
}

- free_OOE;
{
	NX_DURING
		//[theOOEClient free];	// 940824
						// causes timeout
						// why?
		theOOEClient = nil;	
	NX_HANDLER
			NXLogError("ERROR, DOC, OOE: Error %d when "
					"freeing theOOEClient.",
					NXLocalHandler.code);
			
	NX_ENDHANDLER
	if(theOOEClientPath != NULL) {
		NX_FREE(theOOEClientPath);
		theOOEClientPath=NULL;
	}
	return self;
}

- updateOOETarget:sender;
{
	[theOOEClient OOE_documentWasChanged:self];
	[theOOEClient OOE_orderClientDocumentFront:self];
	return self;
}

@end


